package itsm.isperp.module.controller.individuation;

import java.util.List;
import java.util.Map;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.individuation.IddDbEntity;
import itsm.isperp.module.service.individuation.IddDbEntityService;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2017-07-13 15:58
 */
@Controller
@RequestMapping("individuation/dbEntity")
public class IddDbEntityController extends BaseController<IddDbEntity> {

  @Autowired
  protected IddDbEntityService iddDbEntityService;

  @Autowired
  protected JdbcTemplate jdbcTemplate;

  @Override
  public IddDbEntityService getService() {
    return this.iddDbEntityService;
  }

  @Override
  public String getPageTitle() {
    return "流程表单定义";
  }

  @Override
  @RequestMapping("save")
  @ResponseBody
  public ResultMessage save(IddDbEntity model, HttpServletRequest req) {
    
    
    this.getService().saveOrUpdate(model, req);
    return ResultMessage.newInstance(true, null, model.getId());
  }
  
  public ResultMessage createDbTable(String dbName) {
    JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
    // 根据分类ID 获取需要重建的columns
    String ifExists="SHOW TABLES LIKE 'idd_"+dbName+"'";
    List l=jdbcTemplate.queryForList(ifExists);
    if(l.size()>0)
      return ResultMessage.newInstance(false, "已存在该表");
    String sql0 = "select id,data_type from idd_category_property where category_id='" + dbName + "' order by order_num";
    StringBuilder tcolumns = new StringBuilder();
    String columns = "";
    int textAreaSize = 0;
    List<Map<String, Object>> list = jdbcTemplate.queryForList(sql0);
    for (Map<String, Object> li : list) {
      String columnName = li.get("id").toString();
      String dataType = li.get("data_type").toString();
      if (dataType.equals("textarea"))
        textAreaSize++;
      String columnType = "";
      if (dataType.equals("textarea") && textAreaSize < 6) {
        columnType = "VARCHAR(2000)";
      } else if (dataType.equals("textarea") && textAreaSize > 5) {
        columnType = "TEXT";
      } else {
        columnType = "VARCHAR(200)";
      }

      tcolumns.append(columnName + " " + columnType + ",");

    }
    columns = tcolumns.toString();
    String sql = "CREATE TABLE idd_db_" + dbName + "(relation_id VARCHAR(80) NOT NULL," + columns + " PRIMARY KEY (relation_id));";

    try {
      jdbcTemplate.execute(sql);
    } catch (Exception ex) {
      return ResultMessage.newInstance(false, "已存在该表");
    }

    return ResultMessage.newInstance(true);
  }

}
